package com.ittony.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ittony.constant.MessageConstant;
import com.ittony.constant.StatusConstant;
import com.ittony.context.BaseContext;
import com.ittony.dto.DishDTO;
import com.ittony.dto.DishPageQueryDTO;
import com.ittony.entity.Dish;
import com.ittony.entity.DishFlavor;
import com.ittony.entity.SetMeal;
import com.ittony.exception.DeletionNotAllowedException;
import com.ittony.mapper.DishFlavorMapper;
import com.ittony.mapper.DishMapper;
import com.ittony.mapper.SetMealDishMapper;
import com.ittony.mapper.SetMealMapper;
import com.ittony.result.PageResult;
import com.ittony.service.DishFlavorService;
import com.ittony.service.DishService;
import com.ittony.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish, "flavors");

        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());

        dishMapper.addDish(dish);

        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.addDishFlavor(flavors);
        }

    }

    @Override
    public PageResult dishPage(DishPageQueryDTO dishPageQueryDTO) {
        int currentPage = dishPageQueryDTO.getPage();
        int pageSize = dishPageQueryDTO.getPageSize();
        PageHelper.startPage(currentPage, pageSize);

        Page<DishVO> page = dishMapper.dishPage(dishPageQueryDTO);

        long total = page.getTotal();
        List<DishVO> records = page.getResult();
        return new PageResult(total,records);

    }

    @Override
    public void deleteBatch(Long[] dishIds) {
        for (Long dishId : dishIds) {
            Dish dish = dishMapper.getById(dishId);
            if(dish.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        setMealDishMapper.getSetmealIdsByDishIds(dishIds);

        List<Long> setmealIds = setMealDishMapper.getSetmealIdsByDishIds(dishIds);
        if(setmealIds != null && !setmealIds.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        for (Long dishId : dishIds) {
            dishMapper.deleteById(dishId);
            dishFlavorMapper.deleteByDishId(dishId);
        }
    }

    @Override
    public DishVO findDishById(Long id) {
        Dish dish = dishMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);

        List<DishFlavor> dishFlavor = dishFlavorMapper.findByDishId(id);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    public void modifyDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.modifyDish(dish);

        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
       List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            List<DishFlavor> flavors = dishFlavorMapper.findByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
       Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.modifyDish(dish);

        if(status == StatusConstant.DISABLE){
           List<Long> dishIds = new ArrayList<>();
           dishIds.add(id);
            Long[] dishIdsArray = dishIds.toArray(new Long[dishIds.size()]);
            List<Long> setmealIds = setMealDishMapper.getSetmealIdsByDishIds(dishIdsArray);
            if(setmealIds != null && !setmealIds.isEmpty()){
                for(Long setmealId : setmealIds){
                    SetMeal setmeal = SetMeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setMealMapper.update(setmeal);
                }
            }

        }
    }
}
